<?php
    class Curl
    {
        public $responseText = '';    
        
        private $connect_timeout = 2;
        private $evalPHP = false;
        private $method = 'get';
        private $onComplete = '';
        private $onCreate = '';
        private $onFailure = '';
        private $onSuccess = '';
        private $params = '';
        private $timeout = 2;
        private $url = '';

        #---------------------------------------------------------
        
        public function __toString()
        {
            return $this->responseText;
        }
        
        public function __construct( $options )
        {
            foreach( $options as $key => $value )
            {
                $this->$key = $value;
            }
            
            $this->request();
        }
        
        public function request()
        {            
            $curl_handle = curl_init();

            $this->_callback( $this->onCreate );
            
            if( strtolower($this->method) == 'post' )
            {
                curl_setopt( $curl_handle, CURLOPT_URL, $this->url );
                curl_setopt( $curl_handle, CURLOPT_POST, 1 );
                curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, $this->params );
            }
            else
            {
				if( !empty($this->params) )
				{
					$this->params = '?'.$this->params;
				}
                curl_setopt( $curl_handle, CURLOPT_URL, $this->url . $this->params );
                curl_setopt( $curl_handle, CURLOPT_HTTPGET, 1 );
            }

            curl_setopt( $curl_handle, CURLOPT_TIMEOUT, $this->timeout );
            curl_setopt( $curl_handle, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout );        
            curl_setopt( $curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
            curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, 1 );        
        
            $this->responseText = curl_exec( $curl_handle );
            
            if( curl_errno( $curl_handle ) )
            {
                $this->_callback( $this->onFailure );
            }
            else
            {
                $this->_callback( $this->onSuccess );
            }
            
            curl_close( $curl_handle );
            
            $this->_callback( $this->onComplete );
            
        }
        
        #---------------------------------------------------------
        #    Callback Function
        #---------------------------------------------------------
        
        private function _callback( $callback )
        {
            if( is_array($callback) )
            {
                # array must be ( object, (string) method )
                $callback[0]->$callback[1]( $this->responseText );
            }
            else if( empty($callback) )
            {
                return;
            }
            else
            {
                $callback( $this->responseText );
            }
            
            return;
        }
        
    }

?> 